﻿using B.S.BasicData.Api.Read.Application.Command.BaoBiao;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.BaoBiao
{
    public class GetMingxiBaoHandler : IRequestHandler<GetMingxiBaoCommand, ApiResult<List<BaoMingDTO>>>
    {
        private readonly IBaseRepository<UserModel> userRepo;
        private readonly IBaseRepository<ApprovalModel> approvalRepo;

        public GetMingxiBaoHandler(IBaseRepository<UserModel> userRepo,IBaseRepository<ApprovalModel> approvalRepo)
        {
            this.userRepo = userRepo;
            this.approvalRepo = approvalRepo;
        }
        public Task<ApiResult<List<BaoMingDTO>>> Handle(GetMingxiBaoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<BaoMingDTO>> res = new ApiResult<List<BaoMingDTO>>();
            var users = userRepo.GetValue(request.Uid);
            if (users==null)
            {
                res.Msg = "用户不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var approval = approvalRepo.GetAll().Where(x => x.Uid == request.Uid && x.LeaveStatrTime.Month == request.Mouth).Select(x => new BaoMingDTO
            {
                ApprovalStartTime = x.LeaveStatrTime,
                ApprovalEndTime = x.LeaveEndTime,
                CountHours = x.CountHours,
                Reason = x.QingYuan,
                Uid= request.Uid,
                UserName=users.UserName,
                Mouth=request.Mouth,
            }).ToList();
            res.Msg = "查询明细成功";
            res.Code = ApiEnum.Success;
            res.Data= approval;
            return Task.FromResult(res);
            
        }
    }
}
